unit uStockBahanBaku;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, BaseGrid, AdvGrid, ExtCtrls, AdvEdit, AdvCombo,
  RzLabel, RzButton, uDM, DB, MemDS, DBAccess, Uni, RzBckgnd, AdvObj, AdvEdBtn;

type
  TFrmStockBahanBaku = class(TForm)
    Panel1: TPanel;
    KodeStock: TAdvEdit;
    StockName: TAdvEdit;
    NilaiPakai: TAdvEdit;
    grid: TAdvStringGrid;
    RzLabel2: TRzLabel;
    RzLabel1: TRzLabel;
    BtnSave: TRzBitBtn;
    BtnCancel: TRzBitBtn;
    BtnEdit: TRzBitBtn;
    BtnDelete: TRzBitBtn;
    RzBitBtn5: TRzBitBtn;
    RzSeparator1: TRzSeparator;
    RzSeparator2: TRzSeparator;
    SatuanStock: TAdvComboBox;
    SatuanPakai: TAdvComboBox;
    QtyAwal: TAdvEdit;
    Kategori: TAdvEditBtn;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure KategoriKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure RzButton5Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RzBitBtn5Click(Sender: TObject);
    procedure BtnEditClick(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure BtnNewClick(Sender: TObject);
    procedure SatuanStockClick(Sender: TObject);
    procedure SatuanPakaiClick(Sender: TObject);
    procedure BtnCancelClick(Sender: TObject);
    procedure KategoriClickBtn(Sender: TObject);
    procedure KategoriKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    procedure initForm;
    Procedure initGrid;
    Procedure LoadData(ID: String);
    Procedure LoadFromData(ID: String);
    procedure setEnableDisable;
    procedure lookupSatuan;

    function isValid: boolean;
    function isSaved: boolean;
    function getMaxRoom: integer;
  public
    { Public declarations }
  end;

var
  FrmStockBahanBaku: TFrmStockBahanBaku;
CONST
  colNO           = 0;
  colID           = 1;
  colNama         = 2;
  colTipe         = 3;
  colQtyAwal      = 4;
  colNilaiPakai   = 5;
  colSatuanStock  = 6;
  colSatuanPakai  = 7;

implementation

uses uLookup;

{$R *.dfm}

procedure TFrmStockBahanBaku.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TFrmStockBahanBaku.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:= caFree;
end;

procedure TFrmStockBahanBaku.FormShow(Sender: TObject);
begin
  LoadData('');
end;

function TFrmStockBahanBaku.getMaxRoom: integer;
begin
  OpenSQL(DM.Buffer, 'select count(id_room) from room');
  if DM.Buffer.RecordCount > 0 then
    Result := DM.Buffer.Fields[0].AsInteger
  else
    Result := 0;
end;

procedure TFrmStockBahanBaku.initForm;
begin
  KodeStock.Tag := 0;
  KodeStock.Clear;
  Kategori.Clear;
  Kategori.Tag := 0;
  StockName.Clear;
  NilaiPakai.Clear;
  QtyAwal.Clear;
  setEnableDisable;
  lookupSatuan;
  KodeStock.SetFocus;
end;

procedure TFrmStockBahanBaku.initGrid;
begin
  grid.AutoSize:=True;
  grid.Alignments[colNama, 0] := taCenter;
  grid.Alignments[colTipe, 0] := taCenter;
//  grid.Alignments[colIP, 0] := taCenter;
end;

function TFrmStockBahanBaku.isSaved: boolean;
begin
  if KodeStock.Tag = 0 then
  ExecSQL(DM.Buffer, 'insert into stock (KodeStock, NamaStock, IdKategori, '+
  'SatuanStock, SatuanPakai, QtyAwal,NilaiPakai) '+
  'values ('+QuotedStr(KodeStock.Text)+','+QuotedStr(StockName.Text)+','+IntToStr(Kategori.Tag)+','+
  QuotedStr(SatuanStock.Text)+','+QuotedStr(SatuanPakai.Text)+','+QtyAwal.Text+','+NilaiPakai.Text+')')
  else
  ExecSQL(DM.Buffer, 'update stock set '+
  'NamaStock='+QuotedStr(StockName.Text)+','+
  'IDKategori='+IntToStr(Kategori.Tag)+','+
  'SatuanStock='+QuotedStr(SatuanStock.Text)+','+
  'SatuanPakai='+QuotedStr(SatuanPakai.Text)+','+
  'QtyAwal='+QtyAwal.Text+','+
  'NilaiPakai='+NilaiPakai.Text+
  ' where KodeStock='+QuotedStr(KodeStock.Text));
  LoadData('');
end;

function TFrmStockBahanBaku.isValid: boolean;
begin
  Result:= False;
  if CekInput(Result, KodeStock.Text <> '', 'Kode Stock belum diisi.', KodeStock) then
  if CekInput(Result, StockName.Text <> '', 'Nama Stock belum diisi.', StockName) then
  if CekInput(Result, Kategori.Text <> '', 'Kategori belum diisi.', Kategori) then
  if CekInput(Result, SatuanStock.Text <> '', 'Satuan Stock belum diisi.', SatuanStock) then
  if CekInput(Result, SatuanPakai.Text <> '', 'Satuan Pakai belum diisi.', SatuanPakai) then
  if CekInput(Result, NilaiPakai.Text <> '', 'Nilai Pakai belum diisi.', NilaiPakai) then
      CekInput(Result, QtyAwal.Text <> '', 'Qty Awal belum sesuai.', QtyAwal);
end;

procedure TFrmStockBahanBaku.LoadData(ID: String);
var i : integer;
begin
  initForm;

  OpenSQL(DM.Buffer , 'SELECT * from stock s join stock_kategori k on s.IDKategori=k.ID');

  if DM.Buffer.RecordCount > 0 then
  begin
    with grid do
    begin
       for i := 1 to DM.Buffer.RecordCount do
       begin
          RowCount := i + 1;
          Ints[colNO, i] := i;
          Cells[colID, i] := DM.Buffer.Fields[1].AsString;
          Cells[colNama, i] := DM.Buffer.Fields[2].AsString;
          Cells[colTipe, i] := DM.Buffer.Fields[9].AsString;
          Cells[colSatuanStock, i] := DM.Buffer.Fields[4].AsString;
          Cells[colSatuanPakai, i] := DM.Buffer.Fields[5].AsString;
          Cells[colQtyAwal, i] := DM.Buffer.Fields[6].AsString;
          Cells[colNilaiPakai, i] := DM.Buffer.Fields[7].AsString;
            if i mod 2 = 1 then
              RowColor[i] := clMenu;
          DM.Buffer.Next;
       end;
    end;
//    LbInfo.Caption := 'JUMLAH ROOM = '+IntToStr(DM.Buffer.RecordCount);
  end;
  initGrid;
end;


procedure TFrmStockBahanBaku.LoadFromData(ID: String);
begin
  OpenSQL(DM.Buffer , 'SELECT * from stock s join stock_kategori k on s.IDKategori=k.ID where s.KodeStock='+QuotedStr(ID));
  if DM.Buffer.RecordCount > 0 then
  begin
    KodeStock.Text := DM.Buffer.Fields[1].AsString;
    KodeStock.Tag := DM.Buffer.Fields[0].AsInteger;
    StockName.Text := DM.Buffer.Fields[1].AsString;
    Kategori.Text := DM.Buffer.Fields[9].AsString;
    Kategori.Tag := DM.Buffer.Fields[8].AsInteger;
    SatuanStock.Text := DM.Buffer.Fields[4].AsString;
    SatuanPakai.Text := DM.Buffer.Fields[5].AsString;
    QtyAwal.Text := DM.Buffer.Fields[6].AsString;
    NilaiPakai.Text := DM.Buffer.Fields[7].AsString;
  end else
    initForm;
  setEnableDisable;
end;

procedure TFrmStockBahanBaku.lookupSatuan;
begin
  SatuanStock.Clear;
  SatuanPakai.Clear;

  SatuanStock.Items.Add('Add New Item ...');
  SatuanPakai.Items.Add('Add New Item ...');

  OpenSQL(DM.Buffer,'select SatuanStock from stock_satuan');
  if DM.Buffer.RecordCount > 0 then
    while not DM.Buffer.Eof do
    begin
      SatuanStock.Items.Add(DM.Buffer.Fields[0].AsString);
      SatuanPakai.Items.Add(DM.Buffer.Fields[0].AsString);
      DM.Buffer.Next;
    end;
end;

procedure TFrmStockBahanBaku.KategoriClickBtn(Sender: TObject);
begin
  OpenSQL(DM.Lookup, 'select ID, KategoriStock from stock_kategori');
  bukaForm(FrmLookup, TFrmLookup, True, False);
  Kategori.Tag := DM.Lookup.Fields[0].AsInteger;
  Kategori.Text := DM.Lookup.Fields[1].AsString;
end;

procedure TFrmStockBahanBaku.KategoriKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_return then
     SelectNext(ActiveControl, true, true);
end;

procedure TFrmStockBahanBaku.KategoriKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_f2 then
    KategoriClickBtn(Sender);
end;

procedure TFrmStockBahanBaku.BtnSaveClick(Sender: TObject);
begin
  if (isValid)// and (getMaxRoom < 181)
   then isSaved;
end;

procedure TFrmStockBahanBaku.BtnCancelClick(Sender: TObject);
begin
  initForm;
end;

procedure TFrmStockBahanBaku.BtnEditClick(Sender: TObject);
begin
  LoadFromData(grid.Cells[colID, grid.Row]);
end;

procedure TFrmStockBahanBaku.RzBitBtn5Click(Sender: TObject);
begin
  close;
end;

procedure TFrmStockBahanBaku.BtnNewClick(Sender: TObject);
begin
  initForm;
end;

procedure TFrmStockBahanBaku.RzButton5Click(Sender: TObject);
begin
  close;
end;

procedure TFrmStockBahanBaku.SatuanPakaiClick(Sender: TObject);
begin
if SatuanPakai.ItemIndex = 0 then
begin
   OpenSQL(DM.Lookup, 'SELECT SatuanStock as Satuan from stock_satuan');
   LoadFormLookup;
   lookupSatuan;
end;
end;

procedure TFrmStockBahanBaku.SatuanStockClick(Sender: TObject);
begin
if SatuanStock.ItemIndex = 0 then
begin
   OpenSQL(DM.Lookup, 'SELECT SatuanStock as Satuan from stock_satuan');
   LoadFormLookup;
   lookupSatuan;
end;
end;

procedure TFrmStockBahanBaku.setEnableDisable;
begin
  KodeStock.ReadOnly := (KodeStock.Tag = 1);
  BtnCancel.Enabled := (KodeStock.Tag > 0);
  BtnEdit.Enabled := (KodeStock.Tag = 0);
  BtnDelete.Enabled := (KodeStock.Tag > 0);
  QtyAwal.ReadOnly := (KodeStock.Tag > 0);
end;

end.
